﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class AddNewsMaterialItem : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            if (!IsPostBack)
            {
                InitializeContent();
            }
            else
            {
                Save();
            }
        }
        private void Save()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ITransaction tran = session.BeginTransaction();
                try
                {
                    WxMaterial materialDb = new WxMaterial();

                    materialDb.Title = tb_name.Text;
                    materialDb.MsgType = "text";
                    materialDb.CreateDate = DateTime.Now;
                    materialDb.UpdateDate = DateTime.Now;
                    materialDb.Status = "1";
                    materialDb.OwnerAccount = _userAccount;

                    //todo:变成XML
                    materialDb.ResponseXml = "";

                    session.Save(materialDb);

                    WxMaterialText textDb = new WxMaterialText();
                    textDb.Content = tb_content.Text;
                    textDb.Id = materialDb.Id;
                    textDb.OwnerAccount = _userAccount;

                    session.Save(textDb);

                    session.Flush();
                    tran.Commit();
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = _frameHelper.NavTabId
            });
        }

        private void InitializeContent()
        {
        }
    }
}
